package com.swinw.asumall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.swinw.asumall.member.fiegn.CouponFiegnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.swinw.asumall.member.entity.MemberEntity;
import com.swinw.asumall.member.service.MemberService;
import com.swinw.common.utils.PageUtils;
import com.swinw.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员
 *
 * @author 罗义华
 * @email 2407550328@qq.com
 * @date 2020-10-04 13:40:50
 */
@RefreshScope
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Resource
    private CouponFiegnService couponFiegnService;

    /**
     * 用户登录
     * @param memberEntity
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestBody MemberEntity memberEntity){
        return memberService.userLogin(memberEntity);
    }

    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        Object coupons = couponFiegnService.memberCoupon().get("coupons");
        return R.ok().put("member",memberEntity).put("coupons", coupons);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public MemberEntity info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return member;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
        boolean save = memberService.save(member);
        if(save){
            return R.ok();
        }else{
            return R.error(-1,"用户名或电话号码重复");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
